<?php
include 'php/config.php';
include 'php/scripts/utils.php';
session_start();
if (!isset($_SESSION["id_utente"])) {
	header("Location: " . $ISSUER_HOST . "/login.php");
	exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/cupertino/jquery-ui.css" />
<style type="text/css">
.inside {float:left; padding:25px;}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="http://localhost:8888/issuer.js"></script>
<script src="js/upload_badges.js"></script>
<script>
$(function() {
	
	$( "#allBadges" ).accordion({
		collapsible: true,
		active: false,
		heightStyle: "content",
		beforeActivate: function( event, ui ) {}
	});

	$( "#allBadges" ).on( "accordionbeforeactivate", function( event, ui ) {
		$( "#categories" ).accordion({active: false});
		$( "#issuers" ).accordion({active: false});
		$( "#toUpload" ).accordion({active: false});
	});

	$( "#categories" ).accordion({
		collapsible: true,
		active: false,
		heightStyle: "content",
		beforeActivate: function( event, ui ) {}
	});

	$( "#categories" ).on( "accordionbeforeactivate", function( event, ui ) {
		$( "#allBadges" ).accordion({active: false});
		$( "#issuers" ).accordion({active: false});
		$( "#toUpload" ).accordion({active: false});
	});

	$( "#issuers" ).accordion({
		collapsible: true,
		active: false,
		heightStyle: "content",
		beforeActivate: function( event, ui ) {}
	});

	$( "#issuers" ).on( "accordionbeforeactivate", function( event, ui ) {
		$( "#categories" ).accordion({active: false});
		$( "#allBadges" ).accordion({active: false});
		$( "#toUpload" ).accordion({active: false});
	});

	$( "#toUpload" ).accordion({
		collapsible: true,
		active: true,
		heightStyle: "content",
		beforeActivate: function( event, ui ) {}
	});

	$( "#toUpload" ).on( "accordionbeforeactivate", function( event, ui ) {
		$( "#categories" ).accordion({active: false});
		$( "#issuers" ).accordion({active: false});
		$( "#allBadges" ).accordion({active: false});
	});

	$( ".badgeInfo" ).dialog({autoOpen: false, modal: true});

	$( "span[id$='badge']" ).each(function() {
		var badgeToCat = $( this ).clone();
		var badgeToIssuer = $( this ).clone();
		var className = $( this ).attr('class');
		var array = className.split('#');
		if ($( '#noCat' + array[0] ).length)
			$( '#noCat' + array[0] ).remove();
		$( '#cat' + array[0] ).append(badgeToCat);
		if ($( '#noIssuer' + array[1] ).length)
			$( '#noIssuer' + array[1] ).remove();
		$( '#issuer' + array[1] ).append(badgeToIssuer);
		if (array[2] === "0") {
			var badgeToUpload = $( this ).clone();
			if ($( '#badgesToUpload' ).length)
				$( '#noBadgesToUpload' ).remove();
			$( '#badgesToUpload' ).append(badgeToUpload);
		}
	});
});

function showInfo(id) {
	$('#'+id).dialog( "open" );
}
</script>
<title>Open Badges - Pagina Personale </title> 
</head>
<body>
	<h1>Open Badges - Elenco Badges</h1>
	<div>
	<p>Cambio password: <a href="change_password_page.php">Cambia</a></p>
	</div>
	<?php if (!$_SESSION["admin"]) {
		// Connecting, selecting database
		$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
		if ($mysqli->connect_errno) {
			echo "<p>Failed to connect to MySQL: " . $mysqli->connect_error . "<p>";
		} else {
			$query = sprintf($EARNED_BADGES_2, $_SESSION["id_utente"]);
			$result = $mysqli->query($query);
			if (!$result) {
				echo "<p>Failed to run query " . $query . ": (" . $mysqli->errno . ") " .
						$mysqli->error . "<p>";
			} else {
				if (!$result->num_rows) {
					echo "<p>Non ti è stata ancora assegnata alcuna badge</p>";
				} else {
					$categoriesQuery = "SELECT `idCategoria`, `nome` FROM `categoria`";
					$categories = $mysqli->query($categoriesQuery);
					$issuersQuery = "SELECT `issuerId`, `nome` FROM `issuer`";
					$issuers = $mysqli->query($issuersQuery);
					$sharedBadgesQuery = "SELECT `badgeId` FROM `badge` WHERE `condivisa`=TRUE";
					$sharedBadges = $mysqli->query($sharedBadgesQuery);
					if (!$categories || !$issuers || !$sharedBadges) {
						echo "<p>Failed to run select query</p>";
					} else {
						echo '<div id="toUpload">';
						echo '<h3>Badge da caricare</h3>';
						echo '<div id="badgesToUpload">';
						echo '<p id="noBadgesToUpload">Non ci sono badge da caricare</p>';
						echo '</div></div>';
						echo '<div id="allBadges">';
						echo '<h3>Tutte le badges</h3><div>';
						$urls = array();
						$count = 0;
						while ($row = $result->fetch_assoc()) {
							echo generateEarnedBadgeHTML($row, true);
							if (!$row['uploaded']) {
								$singleUrl = "'" . $row["verifyUrl"] . "'";
						 		$urls[] = $singleUrl;
						 		$count++;
							}
						}
						
						echo '</div></div>';
						if ($count != 0) {
							echo "<div>";
							echo sprintf($UPLOAD_BUTTON, " tutte", implode(',', $urls), $_SESSION['id_utente']);
							echo "</div>";
						}
						echo '<h3>Per categoria</h3>';
						echo '<div id="categories">';
						while ($category = $categories->fetch_assoc()) {
							echo '<h3>' . $category['nome'] . '</h3>';
							echo '<div id="cat' . $category['idCategoria'] . '">';
							echo '<p id="noCat' . $category['idCategoria'] . '">Non ci sono badges appartenenti a questa categoria</p>';
							echo '</div>';
						}
						echo '</div>';
						echo '<h3>Per issuer</h3>';
						echo '<div id="issuers">';
						if ($sharedBadges->num_rows) {
							echo '<h3>Badge condivise</h3>';
							echo '<div id="issuer0">';
							echo '<p id="noIssuer0">Non ci sono badges rilasciate da tutte le università</p>';
							echo '</div>';
						}
						while ($issuer = $issuers->fetch_assoc()) {
							echo '<h3>' . $issuer['nome'] . '</h3>';
							echo '<div id="issuer' . $issuer['issuerId'] . '">';
							echo '<p id="noIssuer' . $issuer['issuerId'] . '">Non ci sono badges rilasciate solo da questo issuer</p>';
							echo '</div>';
						}
						echo '</div>';
					}
					
					mysqli_free_result($categories);
					mysqli_free_result($issuers);
					mysqli_free_result($sharedBadges);
				}
			}
			// Free result set
			mysqli_free_result($result);
			
			// Closing connection
			$mysqli->close();
			
		}
	} // end if (!$_SESSION["admin"])
?>
<div>
	<p>Torna alla <a href="index.html">Home</a></p>
</div>
</body>
</html>
